<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="品牌" prop="brandName">
        <!-- <el-input
          v-model="queryParams.brand"
          placeholder="请输入品牌"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-select
          v-model="queryParams.brand"
          placeholder="选择品牌"
          clearable
          filterable
        >
          <el-option
            v-for="dict in dict.type.sys_product_pp"
            :key="dict.value"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="供应商" prop="supplier">
        <!-- <el-input
          v-model="queryParams.supplier"
          placeholder="选择供应商"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-select
          v-model="queryParams.supplier"
          placeholder="选择供应商"
          clearable
          filterable
        >
          <el-option
            v-for="dict in dict.type.sys_product_coder"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="一级类目" prop="firstLevelColumnList">
        <el-select
          v-model="queryParams.firstLevelColumnList"
          placeholder="请选择一级类目"
          multiple
          clearable
        >
          <el-option
            v-for="dict in dict.type.sys_product_pl"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item class="flex_one tr">
        <el-button
          icon="el-icon-search"
          size="mini"
          type="primary"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-button
          :icon="!isOpen ? 'el-icon-arrow-down' : 'el-icon-arrow-up'"
          size="mini"
          @click="shouqi"
          >展开/收起</el-button
        >
      </el-form-item>
      <div v-if="isOpen">
        <el-form-item label="累计下单件数范围" prop="brandName">
          <el-input
            style="width: 100px"
            v-model="queryParams.totalOrderQuantityStart"
            placeholder="最低累计下单件数"
            clearable
            @keyup.enter.native="handleQuery"
          />-<el-input
            style="width: 100px"
            v-model="queryParams.totalOrderQuantityEnd"
            placeholder="最高累计下单件数"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="退货数量范围" prop="brandName">
          <el-input
            style="width: 100px"
            v-model="queryParams.returnQuantityStart"
            placeholder="最低退货数量"
            clearable
            @keyup.enter.native="handleQuery"
          />-<el-input
            style="width: 100px"
            v-model="queryParams.returnQuantityEnd"
            placeholder="最高退货数量"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="金额范围" prop="brandName">
          <el-input
            style="width: 100px"
            v-model="queryParams.totalAmountStart"
            placeholder="最低累计下单件数"
            clearable
            @keyup.enter.native="handleQuery"
          />-<el-input
            style="width: 100px"
            v-model="queryParams.totalAmountEnd"
            placeholder="最高累计下单件数"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="不合格率占比范围" prop="brandName">
          <el-input
            style="width: 100px"
            v-model="queryParams.disqualificationRatioStart"
            placeholder="最低不合格率占比"
            clearable
            @keyup.enter.native="handleQuery"
          />-<el-input
            style="width: 100px"
            v-model="queryParams.disqualificationRatioEnd"
            placeholder="最高不合格率占比"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="返修品已回仓件数范围" prop="brandName">
          <el-input
            style="width: 100px"
            v-model="queryParams.repairedReturnedCountStart"
            placeholder="最低返修品已回仓件数"
            clearable
            @keyup.enter.native="handleQuery"
          />-<el-input
            style="width: 100px"
            v-model="queryParams.repairedReturnedCountEnd"
            placeholder="最高返修品已回仓件数"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
      </div>
    </el-form>
    <timeSearch
      @getDateArr="getDateArr"
      @changDay="changDay"
      :timeArr="timeArr"
      :dayActive="this.dayActive"
      :dayLimit="1095"
    />

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['financial:BiWarehouseDateDwd:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['financial:BiWarehouseDateDwd:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['financial:BiWarehouseDateDwd:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['financial:BiWarehouseDateDwd:export']"
          >导出</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-refresh"
          size="mini"
          @click="handleRefresh"
          >同步</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button plain size="mini" @click="handleinit">初始化</el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row> -->
    <el-row class="mb8" :gutter="10">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >导出
        </el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table
      :data="wmsInventoryList"
      @selection-change="handleSelectionChange"
      :height="630"
      border
      :header-cell-style="{ background: '#027db4', color: '#fff' }"
    >
      <el-table-column label="品牌" align="center" prop="brand" />
      <!-- <el-table-column label="退货发生月份" align="center" prop="monthly"> -->
      <!-- </el-table-column> -->
      <el-table-column label="供应商" align="center" prop="supplier" />
      <!-- <el-table-column label="skc" align="center" prop="skc" /> -->
      <el-table-column label="产品款号" align="center" prop="spu" />
      <el-table-column
        label="累计下单件数"
        align="center"
        prop="totalOrderQuantity"
      >
        <template slot-scope="scope">
          <div
            style="color: rgb(37, 137, 255); cursor: pointer"
            @click="toReturn(scope.row)"
          >
            {{ scope.row.totalOrderQuantity }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="累计入库件数"
        align="center"
        prop="skuStorageQuantity"
      >
        <template slot-scope="scope">
          <div
            style="color: rgb(37, 137, 255); cursor: pointer"
            @click="toReturn(scope.row)"
          >
            {{ scope.row.skuStorageQuantity }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="退货件数" align="center" prop="returnQuantity">
        <template slot-scope="scope">
          <div
            style="color: rgb(37, 137, 255); cursor: pointer"
            @click="goto(scope.row)"
          >
            {{ scope.row.returnQuantity }}
          </div>
        </template>
      </el-table-column>
      <!-- <el-table-column label="退货成本价" align="center" prop="unitPrice" /> -->
      <el-table-column label="退回总金额" align="center" prop="totalAmount" />
      <el-table-column
        label="不合格率占比"
        align="center"
        prop="disqualificationRatio"
      >
        <template scope="scope">
          {{ scope.row.disqualificationRatio }}%
        </template>
      </el-table-column>
      <el-table-column
        label="返修品已回仓件数"
        align="center"
        prop="repairedReturnedCount"
      >
        <template slot-scope="scope">
          <div
            style="color: rgb(37, 137, 255); cursor: pointer"
            @click="toReturn(scope.row, '返修品')"
          >
            {{ scope.row.repairedReturnedCount }}
          </div>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="下单金额占比(占月度总额)"
        align="center"
        prop="orderAmountMonthRatio"
      />
      <el-table-column
        label="下单数量占比(占月度总额)"
        align="center"
        prop="orderQuantityMonthRatio"
      />
      <el-table-column
        label="下单金额占比(占年度总额)"
        align="center"
        prop="orderAmountYearRatio"
      />
      <el-table-column
        label="下单数量占比(占年度总额)"
        align="center"
        prop="orderQuantityYearRatio"
      />

      <el-table-column
        label="超期30天未入库数量"
        align="center"
        prop="overdueQuantity30Days"
      />
      <el-table-column
        label="超入数量"
        align="center"
        prop="overOrderQuantity"
      /> -->
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改仓库日期存储情况对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="进出仓单号" prop="warehouseInOutOrderNum">
          <el-input
            style="width: 100px"
            v-model="form.warehouseInOutOrderNum"
            placeholder="请输入进出仓单号"
          />
        </el-form-item>
        <el-form-item label="采购单号" prop="purchaseOrderNumber">
          <el-input
            style="width: 100px"
            v-model="form.purchaseOrderNumber"
            placeholder="请输入采购单号"
          />
        </el-form-item>
        <el-form-item label="入仓时间" prop="warehouseEntryTime">
          <el-date-picker
            clearable
            v-model="form.warehouseEntryTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择入仓时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="商品编号" prop="productCode">
          <el-input
            style="width: 100px"
            v-model="form.productCode"
            placeholder="请输入商品编号"
          />
        </el-form-item>
        <el-form-item label="出入库数量" prop="inventoryQuantity">
          <el-input
            style="width: 100px"
            v-model="form.inventoryQuantity"
            placeholder="请输入出入库数量"
          />
        </el-form-item>
        <el-form-item label="进出仓金额(含税)" prop="inOutWarehouseAmount">
          <el-input
            style="width: 100px"
            v-model="form.inOutWarehouseAmount"
            placeholder="请输入进出仓金额(含税)"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input
            style="width: 100px"
            v-model="form.remarks"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="外部单号" prop="externalOrderNumber">
          <el-input
            style="width: 100px"
            v-model="form.externalOrderNumber"
            placeholder="请输入外部单号"
          />
        </el-form-item>
        <el-form-item label="日期" prop="dataDate">
          <el-date-picker
            clearable
            v-model="form.dataDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择日期"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="关联采购退货信息"
      :visible.sync="isOpenDwd"
      width="80%"
      v-if="isOpenDwd"
    >
      <PurchaseReturn :query="this.form" />
    </el-dialog>
    <el-dialog
      title="关联采购基础信息"
      :visible.sync="isOpenBase"
      width="80%"
      v-if="isOpenBase"
    >
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExportBase"
          >导出</el-button
        >
      </el-col>
      <Dwd :query="this.form" />
    </el-dialog>
  </div>
</template>

<script>
import Dwd from "@/views/purchase/common/Dwd.vue";
import PurchaseReturn from "@/views/purchase/PurchaseReturn/PurchaseReturn.vue";
import moment from "moment";
import {
  listPurchaseBasicsDwd,
  getPurchaseBasicsDwd,
  delPurchaseBasicsDwd,
  addPurchaseBasicsDwd,
  updatePurchaseBasicsDwd,
  synData,
  selectConditionList,
  selectPurchaseMonth,
  selectPurchaseReturnVoList,
  selectReturnStatisticsList,
} from "@/api/purchase/PurchaseReturn";
import {
  listBiWarehouseDateDwd,
  getBiWarehouseDateDwd,
  delBiWarehouseDateDwd,
  addBiWarehouseDateDwd,
  updateBiWarehouseDateDwd,
  //   synData,
  initData,
} from "@/api/Warehouse/BiWarehouseDateDwd";
import timeSearch from "@/common/day";
export default {
  name: "BiWarehouseDateDwd",
  dicts: [
    "sys_product_qudao",
    "sys_one_zero",
    "sys_product_size",
    "sys_sample_sf",
    "sys_jst_sample_sf",
    "sys_product_pp",
    "sys_product_coder",
    "sys_product_pl",
  ],
  components: { PurchaseReturn, timeSearch, Dwd },
  data() {
    return {
      isOpen: false,
      wmsInventoryList: [],
      dayActive: 30,
      timeArr: [
        { value: "2", label: "昨天" },
        { value: "7", label: "最近7天" },
        { value: "30", label: "最近30天" },
        { value: "365", label: "最近一年" },
        { value: "1", label: "日" },
      ],
      timeGap: { startTime: "", endTime: "" },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仓库日期存储情况表格数据
      BiWarehouseDateDwdList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      isOpenDwd: false,
      isOpenBase: false,
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        warehouseInOutOrderNum: null,
        purchaseOrderNumber: null,
        documentType: null,
        warehouseEntryTime: null,
        productCode: null,
        inventoryQuantity: null,
        inOutWarehouseAmount: null,
        remarks: null,
        externalOrderNumber: null,
        dataDate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        productCode: [
          { required: true, message: "商品编号不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    // this.getList();
  },
  methods: {
    toReturn(row, contractType) {
      if (contractType == "返修品") {
        this.form = {
          pageNum: 1,
          pageSize: 10,
          ...this.timeGap,
          spu: row.spu,
          supplier: row.supplier,
          contractType: "返修品",
        };
      } else {
        this.form = {
          pageNum: 1,
          pageSize: 10,
          ...this.timeGap,
          spu: row.spu,
          supplier: row.supplier,
        };
      }
      this.isOpenBase = true;
    },
    goto(row) {
      this.form = {
        pageNum: 1,
        pageSize: 10,
        ...this.timeGap,
        spu: row.spu,
        supplier: row.supplier,
      };
      this.isOpenDwd = true;
    },
    //搜索栏展示与收起
    shouqi() {
      // console.log(1);
      this.isOpen = !this.isOpen;
    },
    //初始化数据
    handleinit() {
      initData().then((response) => {
        this.$modal.msgSuccess("已初始化数据");
        this.getList();
      });
    },
    // 打开加载层
    openLoading() {
      this.$modal.loading("正在加载数据，请稍候！");
    },
    /** 刷新缓存按钮操作 Warning*/
    handleRefresh() {
      // this.$modal.msgWarning("数据正在同步，请稍候");

      const obj = {
        startTime: this.timeGap.startTime,
        endTime: this.timeGap.endTime,
      };
      this.openLoading();
      synData(obj).then(() => {
        // this.$modal.msgSuccess("同步数据成功");
        this.$modal.closeLoading();
        this.getList();
        // this.$store.dispatch('dict/cleanDict');
      });
    },
    //时间控件
    changDay(val) {
      this.dayActive = val;
      this.timeGap.startTime = val[0];
      this.timeGap.endTime = val[1];
    },
    getDateArr(value) {
      this.timeGap.startTime = value[0];
      this.timeGap.endTime = value[1];
      console.log(JSON.stringify(this.timeGap));
      this.dayActive = 1;
      this.queryParams.startTime = value[0];
      this.queryParams.endTime = value[1];
      selectReturnStatisticsList(this.queryParams).then((response) => {
        this.wmsInventoryList = response.rows ? response.rows : [];
        this.total = response.total;
      });
    },
    /** 查询仓库日期存储情况列表 */
    getList() {
      selectReturnStatisticsList(this.queryParams).then((response) => {
        this.wmsInventoryList = response.rows ? response.rows : [];
        this.total = response.total;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        warehouseInOutOrderNum: null,
        purchaseOrderNumber: null,
        documentType: null,
        warehouseEntryTime: null,
        productCode: null,
        inventoryQuantity: null,
        inOutWarehouseAmount: null,
        remarks: null,
        externalOrderNumber: null,
        dataDate: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams = {};
      this.queryParams.startTime = this.timeGap.startTime;
      this.queryParams.endTime = this.timeGap.endTime;
      this.handleQuery();
      // this.queryParams.brand = "";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加仓库日期存储情况";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getBiWarehouseDateDwd(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改仓库日期存储情况";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateBiWarehouseDateDwd(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBiWarehouseDateDwd(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除仓库日期存储情况编号为"' + ids + '"的数据项？')
        .then(function () {
          return delBiWarehouseDateDwd(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    handleExportBase() {
      delete this.form.pageNum;
      delete this.form.pageSize;
      this.download(
        "purchase/PurchaseBasicsDwd/export",
        {
          ...this.form,
        },
        `PurchaseBasicsDwd_${new Date().getTime()}.xlsx`
      );
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "purchase/PurchaseReturn/exportReturnStatistics",
        {
          ...this.queryParams,
        },
        `exportReturnStatistics${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep input::-webkit-outer-spin-button,
::v-deep input::-webkit-inner-spin-button {
  -webkit-appearance: none !important;
}

// ::v-deep input[type='number'] {
//   // -moz-appearance: textfield !important;
// }

.el-dialog-div {
  height: 50vh;
  overflow-x: hidden;
}

span.textname {
  display: -webkit-box;

  text-overflow: ellipsis;
  overflow: hidden;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
  white-space: pre-wrap;
}

.el-input.is-disabled ::v-deep .el-input__inner {
  color: #606266;
  background-color: white;
  border: none;
}

.el-textarea.is-disabled ::v-deep .el-textarea__inner {
  color: #606266;
  background-color: white;
  border: none;
  width: 500px;
  resize: none;
  cursor: pointer;
}
</style>
/** 累计下单件数范围（该日期区间） */
private Integer totalOrderQuantityStart;
private Integer totalOrderQuantityEnd;

/** 退货数量范围 */
private Integer returnQuantityStart;
private Integer returnQuantityEnd;

/** 金额范围 */
private Integer totalAmountStart;
private Integer totalAmountEnd;

/** 不合格率占比范围（该日期区间） */
private Integer disqualificationRatioStart;
private Integer disqualificationRatioEnd;

/** 返修品已回仓件数范围 */
private Integer repairedReturnedCountStart;
private Integer repairedReturnedCountEnd;